$(function () {
    var sortItemId=0;
    var sortType=0;
    var url = window.location.href;
    var urlZ = url.split("Strategy=");
    var strategyId=urlZ[1];
    $("#strategyId").text("策略ID："+strategyId);
    function getTable(sortItemId,sortType,strategyId) {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Strategy/HedgeStrategy?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Strategy/HedgeStrategy?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
            },
            data: JSON.stringify({
                "strategyId": strategyId,
                "sortItemId":sortItemId,
                "sortType":sortType,
    }),
            success: function (data) {
                // var result = JSON.stringify(data);
                console.log(data)
                if (data.code == 1) {
                    $("#HedTable tbody tr").remove();
                    $("#HedTable").attr({"marketId": data.data.marketId});
                    var _nowDate = getMyDate(data.data.nowDate);//日期
                    var _positionCapital = parseInt(data.data.positionCapital*100)/100;//证券市值
                    var _strategyAvailableCapital = parseInt(data.data.strategyAvailableCapital*100)/100;//资产净值
                    var _strategyCapital = parseInt(data.data.strategyCapital*100)/100;//总资本
                    var _allPositionProfitValue = parseInt(data.data.allPositionProfitValue*100)/100;
                    var _todayAllPositionProfitValue =parseInt(data.data.todayAllPositionProfitValue*100)/100;
                    var _buyOpenValue =parseInt(data.data.buyOpenValue*100)/100;
                    var _sellOpenValue =parseInt(data.data.sellOpenValue*100)/100;
                    var _tips =  data.data.tips;
                    if(_tips!=undefined){
                        var _tipsCount = _tips.length;
                        var tipsChar;
                        for(let o=0;o<_tipsCount;o++){
                            tipsChar +=_tips[o]+'<br>'
                        }
                    }
                    $(".inform").html(tipsChar);
                    $("#sellOpenValue").text("空头市值：" + _sellOpenValue);
                    $("#buyOpenValue").text("多头市值：" + _buyOpenValue);
                    $("#date").text(_nowDate);
                    $("#storeMoney").text(_positionCapital);
                    $("#CMoney").text(_strategyAvailableCapital);
                    $("#allMoney").text(_strategyCapital);
                    $("#allPositionProfitValue").text("累计盈亏：" + _allPositionProfitValue);
                    $("#todayAllPositionProfitValue").text("今日总盈亏：" + _todayAllPositionProfitValue);
                    var count = data.data.strategyPosition.length;
                    for (var i = 0; i < count; i++) {
                        var _group1MonthProfitPer = parseInt(data.data.strategyPosition[i].group1MonthProfitPer * 10000) / 100 + '%'//组合一月涨幅 ,
                        var _group7DayProfitPer = parseInt(data.data.strategyPosition[i].group7DayProfitPer * 10000) / 100 + '%'//组合七日涨幅 ,
                        var _groupId = data.data.strategyPosition[i].groupId//组合序号 ,
                        var _groupProfitValue = parseInt(data.data.strategyPosition[i].groupProfitValue*100)/100//组合盈亏金额 ,
                        var _groupTodayProfitPer = parseInt(data.data.strategyPosition[i].groupTodayProfitPer * 10000) / 100 + '%'//组合今日涨幅 ,
                        var _multiGapRatio = data.data.strategyPosition[i].multiGapRatio//多空比 ,
                        var _strategyPositionItem = data.data.strategyPosition[i].strategyPositionItem //组合条目详细信息
                        var _groupTodayProfitValue = parseInt(data.data.strategyPosition[i].groupTodayProfitValue*100)/100//组合今日盈亏 ,
                        var countGroup = data.data.strategyPosition[i].strategyPositionItem.length;
                        var btnGroup;
                        countGroup == 2 ? btnGroup = "<td><button class='buyView btn btn-color-blue'><i class=' icon-arrow-down'>&nbsp;</i>买入</button>&nbsp;<button class='sellView btn btn-color-red'><i class=' icon-arrow-up'>&nbsp;</i>卖出</button></td>" : btnGroup = "<td><button class='addDouble btn btn-color-blue'>添加对冲</button></td>"
                        if (countGroup == 1 || data.data.strategyPosition[i].strategyPositionItem[0].positionTradeDirection == 1) {
                            for (var j = 0; j < countGroup; j++) {
                                var _detailId = data.data.strategyPosition[i].strategyPositionItem[j].detailId//对冲策略分组id ,
                                var _gmtAssessment = data.data.strategyPosition[i].strategyPositionItem[j].gmtAssessment//评估日期 ,
                                var _gmtCreate = data.data.strategyPosition[i].strategyPositionItem[j].gmtCreate//买入日期 ,
                                var _institutionsAssessment = data.data.strategyPosition[i].strategyPositionItem[j].institutionsAssessment//机构评估 ,
                                var _nowPrice = data.data.strategyPosition[i].strategyPositionItem[j].nowPrice//当前价 ,
                                var _nowValue = parseInt(data.data.strategyPosition[i].strategyPositionItem[j].nowValue*100)/100//当前金额 ,
                                var _positionPrice = data.data.strategyPosition[i].strategyPositionItem[j].positionPrice//持仓成本价 ,
                                var _positionTradeDirection = data.data.strategyPosition[i].strategyPositionItem[j].positionTradeDirection == 1 ? "多头" : "空头"//交易方向多空 1：多头 3：空头 ,
                                var _positionVolume = data.data.strategyPosition[i].strategyPositionItem[j].positionVolume//持仓股数 ,
                                var _profitValue = parseInt(data.data.strategyPosition[i].strategyPositionItem[j].profitValue*100)/100//盈亏金额 ,
                                var _stockId = data.data.strategyPosition[i].strategyPositionItem[j].stockId//股票id ,
                                var _dstockName = data.data.strategyPosition[i].strategyPositionItem[j].stockName//股票名称 ,
                                var _todayProfitPer = parseInt(data.data.strategyPosition[i].strategyPositionItem[j].todayProfitPer * 10000) / 100 + '%'//今日涨幅
                                var _todayProfitValue = parseInt(data.data.strategyPosition[i].strategyPositionItem[j].todayProfitValue*100)/100;//今日盈亏
                                var _type = data.data.strategyPosition[i].strategyPositionItem[j].type==1?'text-danger':'';//底色变红
                                if (j == 0) {
                                    var tr = "<tr class='Tr' _stockId=" + _stockId + " _stockName=" + _dstockName + " _buyPrice=" + _positionPrice + " _myHoldStock=" + _positionVolume + " _myHoldMethod=" + _positionTradeDirection + " _nowPrice=" + _nowPrice + " _detailId=" + _detailId + " _positionTradeDirection=" + _positionTradeDirection + "><td rowspan=" + countGroup + ">" + parseInt(i + 1) + "</td><td class="+_type+">" + _dstockName +"<br/>("+_stockId+ ")</td><td>" + _positionTradeDirection + "</td><td>" + _positionPrice + "</td><td>" + _nowPrice + "</td><td>" + _positionVolume + "</td><td>" + _nowValue + "</td><td>" + _todayProfitValue + "</td><td>" + _todayProfitPer + "</td><td>" + _profitValue + "</td><td rowspan=" + countGroup + ">" + _multiGapRatio + "</td><td rowspan=" + countGroup + ">" + _groupProfitValue + "</td><td rowspan=" + countGroup + ">" + _groupTodayProfitValue + "</td><td rowspan=" + countGroup + ">今日:"+_groupTodayProfitPer+"<br>七日:"+_group7DayProfitPer+"<br>一月:"+ _group1MonthProfitPer+"</td><td>" + getMyDate(_gmtCreate).split(' ')[0] + "</td><td>" + _gmtAssessment + "</td>" + btnGroup + "<td>" + _institutionsAssessment + "</td></tr>";
                                    $("#HedTable tbody").append(tr);
                                }
                                else {
                                    var tr = "<tr class='Tr2' _stockId=" + _stockId + " _stockName=" + _dstockName + " _buyPrice=" + _positionPrice + " _myHoldStock=" + _positionVolume + " _myHoldMethod=" + _positionTradeDirection + " _nowPrice=" + _nowPrice + " _detailId=" + _detailId + " _positionTradeDirection=" + _positionTradeDirection + "><td class="+_type+">" + _dstockName +"<br/>("+_stockId+ ")</td><td>" + _positionTradeDirection + "</td><td>" + _positionPrice + "</td><td>" + _nowPrice + "</td><td>" + _positionVolume + "</td><td>" + _nowValue + "</td><td>" + _todayProfitValue + "</td><td>" + _todayProfitPer + "</td><td>" + _profitValue + "</td><td>" + getMyDate(_gmtCreate).split(' ')[0] + "</td><td>" + _gmtAssessment + "</td>" + btnGroup + "<td>" + _institutionsAssessment + "</td></tr>";
                                    $("#HedTable tbody").append(tr)
                                }

                                // if (j == 0) {
                                //     var tr = "<tr class='Tr' _stockId=" + _stockId + " _stockName=" + _dstockName + " _buyPrice=" + _positionPrice + " _myHoldStock=" + _positionVolume + " _myHoldMethod=" + _positionTradeDirection + " _nowPrice=" + _nowPrice + " _detailId=" + _detailId + " _positionTradeDirection=" + _positionTradeDirection + "><td rowspan=" + countGroup + ">" + parseInt(i + 1) + "</td><td class="+_type+">" + _dstockName +"<br/>("+_stockId+ ")</td><td>" + _positionTradeDirection + "</td><td>" + _positionPrice + "</td><td>" + _nowPrice + "</td><td>" + _positionVolume + "</td><td>" + _nowValue + "</td><td>" + _todayProfitValue + "</td><td>" + _todayProfitPer + "</td><td>" + _profitValue + "</td><td>" + getMyDate(_gmtCreate).split(' ')[0] + "</td><td rowspan=" + countGroup + ">" + _multiGapRatio + "</td><td rowspan=" + countGroup + ">" + _groupProfitValue + "</td><td rowspan=" + countGroup + ">" + _groupTodayProfitValue + "</td><td rowspan=" + countGroup + ">" + _groupTodayProfitPer + "</td><td rowspan=" + countGroup + ">" + _group7DayProfitPer + "</td><td rowspan=" + countGroup + ">" + _group1MonthProfitPer + "</td><td>" + _institutionsAssessment + "</td><td>" + _gmtAssessment + "</td>" + btnGroup + "</tr>";
                                //     $("#HedTable tbody").append(tr);
                                // }
                                // else {
                                //     var tr = "<tr class='Tr2' _stockId=" + _stockId + " _stockName=" + _dstockName + " _buyPrice=" + _positionPrice + " _myHoldStock=" + _positionVolume + " _myHoldMethod=" + _positionTradeDirection + " _nowPrice=" + _nowPrice + " _detailId=" + _detailId + " _positionTradeDirection=" + _positionTradeDirection + "><td class="+_type+">" + _dstockName +"<br/>("+_stockId+ ")</td><td>" + _positionTradeDirection + "</td><td>" + _positionPrice + "</td><td>" + _nowPrice + "</td><td>" + _positionVolume + "</td><td>" + _nowValue + "</td><td>" + _todayProfitValue + "</td><td>" + _todayProfitPer + "</td><td>" + _profitValue + "</td><td>" + getMyDate(_gmtCreate).split(' ')[0] + "</td><td>" + _institutionsAssessment + "</td><td>" + _gmtAssessment + "</td>" + btnGroup + "</tr>";
                                //     $("#HedTable tbody").append(tr)
                                // }
                            }
                        }
                        else {
                            // var _detailId = _gmtAssessment = _gmtCreate = _gmtCreate = _institutionsAssessment = _nowPrice = _nowValue = _positionPrice = _positionPrice = _positionTradeDirection = _positionVolume = _positionVolume = _profitValue = _stockId = _dstockName = _todayProfitPer = _todayProfitValue = [];
                            var _detailId =[];
                            var _gmtAssessment= [];
                            var _gmtCreate= [];
                            var _institutionsAssessment= [];
                            var _nowPrice= [];
                            var _nowValue= [];
                            var _positionPrice= [];
                            var _positionTradeDirection= [];
                            var _positionVolume= [];
                            var _profitValue= [];
                            var _stockId= [];
                            var _dstockName= [];
                            var _todayProfitPer= [];
                            var _todayProfitValue= [];
                            var _type = [];

                            for (var j = 0; j < countGroup; j++) {
                                //对冲策略分组id ,
                                _detailId.push(data.data.strategyPosition[i].strategyPositionItem[j].detailId)
                                _gmtAssessment.push(data.data.strategyPosition[i].strategyPositionItem[j].gmtAssessment);//评估日期 ,
                                _gmtCreate.push(data.data.strategyPosition[i].strategyPositionItem[j].gmtCreate);//买入日期 ,
                                _institutionsAssessment.push(data.data.strategyPosition[i].strategyPositionItem[j].institutionsAssessment);//机构评估 ,
                                _nowPrice.push(data.data.strategyPosition[i].strategyPositionItem[j].nowPrice);//当前价 ,
                                _nowValue.push(parseInt(data.data.strategyPosition[i].strategyPositionItem[j].nowValue*100)/100);//当前金额 ,
                                _positionPrice.push(data.data.strategyPosition[i].strategyPositionItem[j].positionPrice);//持仓成本价 ,
                                _positionTradeDirection.push(data.data.strategyPosition[i].strategyPositionItem[j].positionTradeDirection == 1 ? "多头" : "空头");//交易方向多空 1：多头 3：空头 ,
                                _positionVolume.push(data.data.strategyPosition[i].strategyPositionItem[j].positionVolume);//持仓股数 ,
                                _profitValue.push(parseInt(data.data.strategyPosition[i].strategyPositionItem[j].profitValue*100)/100);//盈亏金额 ,
                                _stockId.push(data.data.strategyPosition[i].strategyPositionItem[j].stockId);//股票id ,
                                _dstockName.push(data.data.strategyPosition[i].strategyPositionItem[j].stockName);//股票名称 ,
                                _todayProfitPer.push(parseInt(data.data.strategyPosition[i].strategyPositionItem[j].todayProfitPer * 10000) / 100 + '%');//今日涨幅
                                _todayProfitValue.push(parseInt(data.data.strategyPosition[i].strategyPositionItem[j].todayProfitValue*100)/100);//今日盈亏
                                _type.push(data.data.strategyPosition[i].strategyPositionItem[j].type==1?'text-danger':'');//底色变红
                            }
                            var tr = "<tr class='Tr' _stockId=" + _stockId[1] + " _stockName=" + _dstockName[1] + " _buyPrice=" + _positionPrice[1] + " _myHoldStock=" + _positionVolume[1] + " _myHoldMethod=" + _positionTradeDirection[1] + " _nowPrice=" + _nowPrice[1] + " _detailId=" + _detailId[1] + " _positionTradeDirection=" + _positionTradeDirection[1] + "><td rowspan=" + countGroup + ">" + parseInt(i + 1) + "</td><td class="+_type[1]+">" + _dstockName[1]+"<br/>("+_stockId[1] + ")</td><td>" + _positionTradeDirection[1] + "</td><td>" + _positionPrice[1] + "</td><td>" + _nowPrice[1] + "</td><td>" + _positionVolume[1] + "</td><td>" + _nowValue[1] + "</td><td>" + _todayProfitValue[1] + "</td><td>" + _todayProfitPer[1] + "</td><td>" + _profitValue[1] + "</td><td rowspan=" + countGroup + ">" + _multiGapRatio + "</td><td rowspan=" + countGroup + ">" + _groupProfitValue + "</td><td rowspan=" + countGroup + ">" + _groupTodayProfitValue + "</td><td rowspan=" + countGroup + ">今日:"+_groupTodayProfitPer+"<br>七日:"+_group7DayProfitPer+"<br>一月:"+ _group1MonthProfitPer+"</td><td>" + getMyDate(_gmtCreate[1]).split(' ')[0] + "</td><td>" + _gmtAssessment[1] + "</td>" + btnGroup + "<td>" + _institutionsAssessment[1] + "</td></tr>";
                            $("#HedTable tbody").append(tr);
                            var tr2 = "<tr class='Tr2' _stockId=" + _stockId[0] + " _stockName=" + _dstockName[0] + " _buyPrice=" + _positionPrice[0] + " _myHoldStock=" + _positionVolume[0] + " _myHoldMethod=" + _positionTradeDirection[0] + " _nowPrice=" + _nowPrice[0] + " _detailId=" + _detailId[0] + " _positionTradeDirection=" + _positionTradeDirection[0] + "><td class="+_type[0]+">" + _dstockName[0] + "<br/>("+_stockId[0] + ")</td><td>" + _positionTradeDirection[0] + "</td><td>" + _positionPrice[0] + "</td><td>" + _nowPrice[0] + "</td><td>" + _positionVolume[0] + "</td><td>" + _nowValue[0] + "</td><td>" + _todayProfitValue[0] + "</td><td>" + _todayProfitPer[0] + "</td><td>" + _profitValue[0] + "</td><td>" + getMyDate(_gmtCreate[0]).split(' ')[0] + "</td><td>" + _gmtAssessment[0] + "</td>" + btnGroup+ "<td>" + _institutionsAssessment[0] + "</td></tr>";
                            $("#HedTable tbody").append(tr2);

                            // var tr = "<tr class='Tr' _stockId=" + _stockId[1] + " _stockName=" + _dstockName[1] + " _buyPrice=" + _positionPrice[1] + " _myHoldStock=" + _positionVolume[1] + " _myHoldMethod=" + _positionTradeDirection[1] + " _nowPrice=" + _nowPrice[1] + " _detailId=" + _detailId[1] + " _positionTradeDirection=" + _positionTradeDirection[1] + "><td rowspan=" + countGroup + ">" + parseInt(i + 1) + "</td><td class="+_type[1]+">" + _dstockName[1]+"<br/>("+_stockId[1] + ")</td><td>" + _positionTradeDirection[1] + "</td><td>" + _positionPrice[1] + "</td><td>" + _nowPrice[1] + "</td><td>" + _positionVolume[1] + "</td><td>" + _nowValue[1] + "</td><td>" + _todayProfitValue[1] + "</td><td>" + _todayProfitPer[1] + "</td><td>" + _profitValue[1] + "</td><td>" + getMyDate(_gmtCreate[1]).split(' ')[0] + "</td><td rowspan=" + countGroup + ">" + _multiGapRatio + "</td><td rowspan=" + countGroup + ">" + _groupProfitValue + "</td><td rowspan=" + countGroup + ">" + _groupTodayProfitValue + "</td><td rowspan=" + countGroup + ">" + _groupTodayProfitPer + "</td><td rowspan=" + countGroup + ">" + _group7DayProfitPer + "</td><td rowspan=" + countGroup + ">" + _group1MonthProfitPer + "</td><td>" + _institutionsAssessment[1] + "</td><td>" + _gmtAssessment[1] + "</td>" + btnGroup + "</tr>";
                            // $("#HedTable tbody").append(tr);
                            // var tr2 = "<tr class='Tr2' _stockId=" + _stockId[0] + " _stockName=" + _dstockName[0] + " _buyPrice=" + _positionPrice[0] + " _myHoldStock=" + _positionVolume[0] + " _myHoldMethod=" + _positionTradeDirection[0] + " _nowPrice=" + _nowPrice[0] + " _detailId=" + _detailId[0] + " _positionTradeDirection=" + _positionTradeDirection[0] + "><td class="+_type[0]+">" + _dstockName[0] + "<br/>("+_stockId[0] + ")</td><td>" + _positionTradeDirection[0] + "</td><td>" + _positionPrice[0] + "</td><td>" + _nowPrice[0] + "</td><td>" + _positionVolume[0] + "</td><td>" + _nowValue[0] + "</td><td>" + _todayProfitValue[0] + "</td><td>" + _todayProfitPer[0] + "</td><td>" + _profitValue[0] + "</td><td>" + getMyDate(_gmtCreate[0]).split(' ')[0] + "</td><td>" + _institutionsAssessment[0] + "</td><td>" + _gmtAssessment[0] + "</td>" + btnGroup+ "</tr>";
                            // $("#HedTable tbody").append(tr2);

                        }

                        $("#HedTable tbody").append("<tr><td colspan='20'></td></tr>")
                    }
                    buyAndSell();
                }


                if (data.code != 1) {
                    alert(data.message)
                }
            },
            error: function () {
                alert('请求持仓列表失败');
            }
        })
    };
    getTable(sortItemId,sortType,strategyId);

    //排序
    $(".sort_down ,.sort_up").click(function () {
        var val = $(this).attr("value");
        $(".sort_down ,.sort_up").removeClass("imgIn");
        $(this).addClass("imgIn");
        getTable(parseInt(val[0]),parseInt(val[2]),1108);
    });

    //表头固定
    var tableCont = document.querySelector('.Hedtable');
    tableCont.addEventListener('scroll', scrollHandle);

    // /Back/Strategy/HedgeBuy
    // 新增股票组
    $("#addNewGroup").click(function () {
        $(".business").fadeOut();
        $("#buyView").attr({"detailId": "0"});
        $("#buyView input").val("");
        $("#buyStockId").removeAttr("readonly");
        $("#buyStockName").removeAttr("readonly");
        var timestamp = new Date().getTime();
        var timeNow = new Date(timestamp).Format("yyyy-MM-ddThh:mm:ss");
        $("#buyTradeDirection").removeAttr("disabled");
        $("#buyTime").val(timeNow);
        $("#buy_range_slider").val(0);
        $("#buy_range_slider").css({"background-size": '0% 100%'});
        $("#canBuyMoney").html("可用:" + $("#CMoney").text());
        $("#canBuyMoney").attr({"Money": $("#CMoney").text()});
        $("#buyView").fadeIn();
    });

    //买入卖出
    function buyAndSell() {
        $(".buyView").click(function () {
            $("#buyView input").val("");
            var _detailId = $(this).parent().parent().attr("_detailId");
            var _stockId = $(this).parent().parent().attr("_stockId");
            var _stockName = $(this).parent().parent().attr("_stockName");
            var _buyPrice = $(this).parent().parent().attr("_nowPrice");
            $("#buyStockId").val(_stockId);
            var positionTradeDirection = $(this).parent().parent().attr("_positionTradeDirection");
            positionTradeDirection == '空头' ? positionTradeDirection = "2" : positionTradeDirection = "1";
            $("#buyTradeDirection").attr({"disabled": "disabled"});
            $("#buyTradeDirection").val(positionTradeDirection);
            $("#buyStockName").val(_stockName);
            $("#buyPrice").val(_buyPrice);
            $("#canBuyMoney").html("可用:" + $("#CMoney").text());
            $("#canBuyMoney").attr({"Money": $("#CMoney").text()});
            var timestamp = new Date().getTime();
            var timeNow = new Date(timestamp).Format("yyyy-MM-ddThh:mm:ss");
            $("#buyTime").val(timeNow);
            $("._h2strategyName").html($(".hold_position").attr("_name"));
            $("#buy_range_slider").val(0);
            $("#buy_range_slider").css({"background-size": '0% 100%'});
            $("#buyStockName").attr({"readonly": "readonly"});
            $("#buyStockId").attr({"readonly": "readonly"});
            $("#buyView").attr({"detailId": _detailId});
            $(".business").fadeOut();
            $("#buyView").fadeIn()
        });
        $(".sellView").click(function () {
            $("#sellView input").val("");
            var _detailId = $(this).parent().parent().attr("_detailId");
            var _stockId = $(this).parent().parent().attr("_stockId");
            var _stockName = $(this).parent().parent().attr("_stockName");
            var _buyPrice = $(this).parent().parent().attr("_buyPrice");
            var _nowPrice = $(this).parent().parent().attr("_nowPrice");
            var _myHoldStock = $(this).parent().parent().attr("_myHoldStock");
            var _myHoldMethod = $(this).parent().parent().attr("_myHoldMethod");
            var myHoldStock = $(this).parent().parent().attr("myHoldStock");
            var timestamp = new Date().getTime();
            var timeNow = new Date(timestamp).Format("yyyy-MM-ddThh:mm:ss");
            var positionTradeDirection = $(this).parent().parent().attr("_positionTradeDirection");
            // positionTradeDirection=='空头'?positionTradeDirection="2":positionTradeDirection="1";
            // $("#buyTradeDirection").attr({"disabled":"disabled"});
            // $("#buyTradeDirection").val(positionTradeDirection);
            $("#sellTime").val(timeNow);
            $("._h2strategyName").html($(".hold_position").attr("_name"))
            $("#sellStockId").val(_stockId);
            $("#sellstockName").val(_stockName);
            $("#myHoldStock").val(_myHoldStock);
            $("#holdPrice").val(_buyPrice);
            $("#sellPrice").val(_nowPrice);
            $("#myHoldMethod").val(_myHoldMethod);
            $("#sell_range_slider").val(0);
            $("#sell_range_slider").css({"background-size": '0% 100%'});
            $("#sellView").attr({"detailId": _detailId});
            $(".business").fadeOut();
            $("#sellView").fadeIn();
        })
        $(".addDouble").click(function () {
            $(".business").fadeOut();
            $("#buyView input").val("");
            var _detailId = $(this).parent().parent().attr("_detailId");
            var positionTradeDirection = $(this).parent().parent().attr("_positionTradeDirection");
            positionTradeDirection == '空头' ? positionTradeDirection = "1" : positionTradeDirection = "2";
            $("#buyTradeDirection").attr({"disabled": "disabled"});
            $("#buyTradeDirection").val(positionTradeDirection);
            $("#buyStockId").removeAttr("readonly");
            $("#buyStockName").removeAttr("readonly");
            var timestamp = new Date().getTime();
            var timeNow = new Date(timestamp).Format("yyyy-MM-ddThh:mm:ss");
            $("#buyTime").val(timeNow);
            $("#buy_range_slider").val(0);
            $("#buy_range_slider").css({"background-size": '0% 100%'});
            $("#canBuyMoney").html("可用:" + $("#CMoney").text());
            $("#canBuyMoney").attr({"Money": $("#CMoney").text()});
            $("#buyView").attr({"detailId": _detailId});
            $("#buyView").fadeIn();
        })
    }

    //买入卖出联动
    $("#buyPrice,#buyCount").blur(function () {
        var _buyPrice = $("#buyPrice").val()
        var _buyCount = $("#buyCount").val();
        _buyPrice != '' && _buyCount != '' ? $("#buySum").val(_buyPrice * _buyCount) : console.log("请填写买入量");
    });

    $("#sellPrice,#sellCount").blur(function () {
        var _sellPrice = $("#sellPrice").val();
        var _sellCount = $("#sellCount").val();
        var _myHoldStock = $("#myHoldStock").val();
        _sellPrice != '' && _sellCount != '' ? $("#sellSum").val(_sellCount * _sellPrice) : console.log("请填写买入量");
        $("#sellProportion").val(Math.ceil(_sellCount / _myHoldStock * 100) + '%');
        $("#sell_range_slider").val(parseFloat($("#sellProportion").val()));
        $("#sell_range_slider").css({"background-size": $("#sellProportion").val() + '100%'});
    });

    //买入比例条
    $("#buy_range_slider").bind('input porpertychange', function () {
        if ($("#buyPrice").val() != '') {
            $("#buyProportion").val($("#buy_range_slider").val() + '%');
            $("#buyCount").val(Math.floor($("#buy_range_slider").val() / 100 * $("#canBuyMoney").attr('Money') / $("#buyPrice").val()));
            var sum = $("#buyPrice").val() * $("#buyCount").val();
            $("#buySum").val(sum);
        }
    });
    //卖出比例条
    $("#sell_range_slider").bind('input porpertychange', function () {
        $("#sellProportion").val($("#sell_range_slider").val() + '%');
        var sellCount = parseInt(parseFloat($("#sell_range_slider").val()) / 100 * $("#myHoldStock").val())
        $("#sellCount").val(sellCount);
        $("#sellSum").val(sellCount * $("#sellPrice").val());
//逻辑部分
    });
    // 手动输入卖出比例
    $("#sellProportion").blur(function () {
        $("#sellProportion").val(parseFloat($("#sellProportion").val()) + '%');
        var sellCount = parseInt(parseFloat($("#sellProportion").val()) / 100 * $("#myHoldStock").val());
        $("#sell_range_slider").val(parseFloat($("#sellProportion").val()));
        $("#sell_range_slider").css({"background-size": $("#sellProportion").val() + '100%'});
        $("#sellCount").val(sellCount);
        $("#sellSum").val(sellCount * $("#sellPrice").val());
    });
    //手动买入
    $("#buyProportion").blur(function () {
        if ($("#buyPrice").val() != '' && $("#buyProportion").val() != '') {
            $("#buyProportion").val(parseFloat($("#buyProportion").val()) + '%');
            $("#buy_range_slider").val(parseFloat($("#buyProportion").val()));
            $("#buy_range_slider").css({"background-size": $("#buyProportion").val() + '100%'});
            var buyCount = Math.floor($("#buy_range_slider").val() / 100 * $("#canBuyMoney").attr('Money') / $("#buyPrice").val());
            $("#buyCount").val(buyCount);
            $("#buySum").val($("#buyCount").val() * $("#buyPrice").val());
        }
        else {
            $("#buyProportion").val('');
            alert("请先输入买入价");
        }
    });

//关闭买卖框

    $("#buyViewClose").click(function () {
        $(".business").fadeOut()

    });
    $("#SellViewClose").click(function () {
        $(".business").fadeOut()
    });

    //发送买入请求
    $("#buyNow").click(function () {
        var moneyNow = ($(".hold_position").attr("_money") - $("#buySum").val()).toFixed(2);
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Strategy/HedgeBuy?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Strategy/HedgeBuy?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
            },
            data: JSON.stringify({
                "detailId": $("#buyView").attr("detailId"),
                "marketId": $("#HedTable").attr('marketId'),
                "stockId": $("#buyStockId").val(),
                "stockName": $("#buyStockName").val(),
                "strategyId": 1108,
                "tradeDate": $("#buyTime").val().split("T").join(" "),
                "tradeDirection": $("#buyTradeDirection option:selected").val(),
                "tradePrice": parseFloat($("#buyPrice").val()),
                "tradeVolume": parseInt($("#buyCount").val()),
            }),
            success: function (data) {
                // var result = JSON.stringify(data);
                console.log(data)
                if (data.code == 1) {
                    $(".business").fadeOut();
                    setTimeout(function () {
                        $(".business input").val('')
                    }, 200);
                    getTable(sortItemId,sortType,strategyId);
                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('请求持仓列表失败');
            }
        })
    })
    //发送卖出请求
    $("#sellNow").click(function () {
        var tradeDirection = $("#myHoldMethod").val() == '空头' ? 4 : 3;
        var moneyNow = (parseFloat($("#sellSum").val()) + parseFloat($(".hold_position").attr("_money"))).toFixed(2);
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Strategy/HedgeSell?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Strategy/HedgeSell?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
            },
            data: JSON.stringify({
                "detailId": $("#sellView").attr("detailId"),
                "marketId": $("#HedTable").attr('marketId'),
                "stockId": $("#sellStockId").val(),
                "stockName": $("#sellstockName").val(),
                "strategyId": 1108,
                "tradeDate": $("#sellTime").val().split("T").join(" "),
                "tradeDirection": tradeDirection,
                "tradePrice": parseFloat($("#sellPrice").val()),
                "tradeVolume": parseInt($("#sellCount").val()),
            }),
            success: function (data) {
                // var result = JSON.stringify(data);
                console.log(data)
                if (data.code == 1) {
                    $(".business").fadeOut();
                    setTimeout(function () {
                        $(".business input").val('')
                    }, 200);
                    getTable(sortItemId,sortType,strategyId);

                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('请求持仓列表失败');
            }
        })
    });

    function checkView() {
        $(".checkboxDiv input:checkbox").change(function () {
            var val = $(this).val().split(",");
            var check = $(this).attr('checked');
            if(check){
                $(this).removeAttr("checked");
                if(val.length==2){
                    $(".Tr").each(function () {
                        $(this).find('td').eq(val[0]).hide();
                        $(this).find('th').eq(val[0]).hide();
                    })
                    $(".Tr2").each(function () {
                        $(this).find('td').eq(val[1]).hide();
                        $(this).find('th').eq(val[1]).hide();
                    })
                }else {
                    $(".Tr").each(function () {
                        $(this).find('th').eq(val).hide();
                        $(this).find('td').eq(val).hide();
                    });
                }
            }
            else {
                $(this).attr({"checked": "checked"});
                if (val.length == 2) {
                    $(".Tr").each(function () {
                        $(this).find('th').eq(val[0]).show();
                        $(this).find('td').eq(val[0]).show();
                    });
                    $(".Tr2").each(function () {
                        $(this).find('th').eq(val[1]).show();
                        $(this).find('td').eq(val[1]).show();
                    })
                }
                else {
                    $(".Tr").each(function () {
                        $(this).find('th').eq(val).show();
                        $(this).find('td').eq(val).show();
                    });
                }
            }
        })
    };
    checkView()
})